import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getMenus } from '@/api/system/menu'

export const useMenuStore = defineStore('menu', () => {
  // state
  const list = ref(undefined)

  // actions
  const setMenu = (data) => {
    list.value = data
  }

  const clearMenu = () => {
    list.value = undefined
  }

  // 获取用户菜单信息
  const getMenu = () => {
    return new Promise((resolve, reject) => {
      getMenus()
        .then((response) => {
          const { data } = response
          setMenu(data)
          resolve(data)
        })
        .catch((error) => {
          reject(error)
        })
    })
  }

  return {
    list,
    setMenu,
    clearMenu,
    getMenu
  }
})
